import requests
import json

class AccessToken:
    host_cla = 'https://aip.baidubce.com/oauth/2.0/\
token?grant_type=client_credentials&client_id=\
{}&client_secret={}'#获取access_token值的链接

    header = {'Content-Type': 'application/json; charset=UTF-8'}
    ak = 'w6qz0P7KaA6Z2aWFgHSGWoD8'
    sk = 'YctVEgyHYmAt310mp6o1iS5L6NKqkenz'
    access_token = requests.get(
            host_cla.format(ak,sk),
            headers = header
            ).json()['access_token']
        
    def __init__(self,
                 img_path = None,
                 img_url = None,
                 face_token = None,
                 ):
        
        print(self.access_token)
        if img_path != None:
            img_64 = self.base64_image(img_path)
            self.image,self.image_type = img_64,"BASE64"
            
        elif img_url != None:
            self.image,self.image_type = img_url,"URL"
            
        elif face_token != None:
            self.image,self.image_type = face_token,"FACE_TOKEN"

    def base64_image(self,img_path):
        '''\
        img_path :图片连接
        return :  返回图片的base64编译字符串
        '''
        import base64
        with open(img_path,'rb') as f:
            base64_data = base64.b64encode(f.read())

        return str(base64_data,'utf-8')


    def info(self,url,params):
        info_return = requests.post(url,
                                    headers = self.header,
                                    data = json.dumps(params),
                                    ).json()

        return info_return

    




#人脸检测
class DetectFace(AccessToken):


    #expression笑脸检测  0不笑，1微笑，2大笑
    #faceshape 脸型置信度。
    #gender 检测性别  male男，female女。
    #glasses 是否带眼镜，0-无眼镜，1-普通眼镜，2-墨镜。
    #landmark  4个关键点位置，左眼中心、右眼中心、鼻尖、嘴中心
    #race  种族 yellow、white、black、arabs。
    #qualities 人脸质量信息

    def detect_face(self):

        url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?\
access_token={}'.format(self.access_token)
        params = {"image":self.image,
             "image_type":self.image_type,
             "face_field":"faceshape,facetype,beauty,age,glasses"}

        
        return_info = self.info(url,params)
        return return_info

  

#人脸对比
class ComparFace(AccessToken):

    def __init__(self,
                 img_path1 = None,
                 img_url1 = None,
                 face_token1 = None,
                 img_path2 = None,
                 img_url2 = None,
                 face_token2 = None,
                 ):
        
        if img_path1 != None:
            img_641 = self.base64_image(img_path1)
            self.image1,self.image_type1 = img_641,"BASE64"
            
        elif img_url1 != None:
            self.image1,self.image_type1 = img_url1,"URL"
            
        elif face_token1 != None:
            self.image1,self.image_type1 = face_token1,"FACE_TOKEN"



        if img_path2 != None:
            img_642 = self.base64_image(img_path2)
            self.image2,self.image_type2 = img_642,"BASE64"
            
        elif img_url2 != None:
            self.image2,self.image_type2 = img_url2,"URL"
            
        elif face_token2 != None:
            self.image2,self.image_type2 = face_token2,"FACE_TOKEN"


    def compar_face(self):
        url = 'https://aip.baidubce.com/rest/2.0/face/v3/match?\
access_token={}'.format(self.access_token)
        params = [

         {"image": self.image1,
          "image_type": self.image_type1,
          "face_type": "LIVE",
          "quality_control": "LOW"},

         {"image": self.image2,
          "image_type": self.image_type2,
          "face_type": "IDCARD",
          "quality_control": "LOW"}
         ]

        
        return_info = self.info(url,params)
        return return_info

        
#人脸数据库管理  
class FaceDataBase(AccessToken):

    #人脸添加
    def face_add(self,
                       user_id,
                       user_info,
                       group_id = "MrZhao",
                       quality_control = "LOW",
                       liveness_control = "NORMAL",
                       ):

        url = 'https://aip.baidubce.com/rest/2.0/face/v3/\
faceset/user/add?access_token={}'.format(self.access_token)
        

        params = {"image":self.image,
                  "image_type":self.image_type,
                  "group_id":group_id,
                  "user_id":user_id,
                  "user_info":user_info,
                  "quality_control":quality_control,
                  "liveness_control":liveness_control}
        return_info = self.info(url,params)
        return return_info



    #人脸搜索
    def face_search(self,group_id_list = "MrZhao"):
        
        url = 'https://aip.baidubce.com/rest/2.0/face/v3/search?\
access_token={}'.format(self.access_token)
        
        params = {"image":self.image,
                  "image_type":self.image_type,
                  "group_id_list":group_id_list,
                  "quality_control":"LOW",
                  "liveness_control":"NONE"}

        return_info = self.info(url,params)
        return return_info


    #人脸更新
    def face_update(self):
        pass
        




if __name__ == "__main__":
    image_path1 = 'C:/Users/赵孟/Desktop/5.jpg'
    image_path2 = 'C:/Users/赵孟/Desktop/7.jpg'
    x = ComparFace(img_path1 = image_path2,img_path2 = image_path2)
    info = x.compar_face()
    print(info)

    


















